<?php
session_start();
include 'config.php';
include 'functions.php';
$userid = $_SESSION['userid'];
	
$lexid = $_GET['id'];
$_SESSION['slexid'] = $lexid;

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}


$currpage =curPageURL();
$check = strpbrk($currpage, '1234567890');
if ($check == null){
	echo "no number";
header("location:lex.php");	
}





if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['commentSubmit'])){	
$comment = $_POST['tbComment'];
$userid = $_SESSION['userid'];
$lexid = $_GET['id'];
$lexid = $_POST['tbLexid'];
$_SESSION['slexid'] = $lexid;

$query0 = "INSERT INTO comments (comment, lexid, commenter) VALUES ('$comment','$lexid','$userid')";
$result = mysqli_query($con, $query0) or die("Could not record comment in database");
$_POST['tbComment'] = "";
 header("Location: ilex.php?id=".$lexid."");
}
}
$query = "SELECT  *	FROM  map	LEFT JOIN reflection USING (lexid) 
						LEFT JOIN comments USING (lexid) 
						LEFT JOIN lexsettings USING (lexid) 
						LEFT JOIN files USING (lexid) 
						WHERE lexid = '$goid'";
						
$result = mysqli_query($con, $query) or die();
$row = mysqli_fetch_array($result);
/*print_r($row);*/
	extract($row);
$_SESSION['stitle'] = $title;		
$time = strtotime($created);
    	$formatToday = date("D d F Y");
   		$formatdate  = date("D d F Y", $time);
     if($formatToday == $formatdate){
        $formatdate = "Today";
    	}
	$formattime =  date("g:ia", $time);	
    $datetime = "$formatdate at $formattime";
/*	if ($fileurl !=""){
	$fileurl = "<img src='".$fileurl."'/>";
	 }	
	 else {
	$fileurl = "" ;
		 }*/
$ltitle = str_replace(' ', '_', $title);
$reflectionWc = str_word_count($reflection);


		?>
<!doctype HTML>
<title>My Lex</title>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800' rel='stylesheet' type='text/css'>
<link href="styles/styles.css" type="text/css" rel="stylesheet" />
<link href='http://fonts.googleapis.com/css?family=Oxygen:400,300,700&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<script src="js/main.js"></script>
</head>
<body class="bodyBg">
<?php include_once("topNav.php"); ?>
<?php include_once("mainNav.php"); ?>
<?php include_once("mainFooter.php"); ?>
<div id="content">
  <div class="ilexmain inlineb">
    <div id="map">
      <iframe src="http://www.lorettarose.co.uk/pinpoint/createmap.php"></iframe>
    </div>
    <!------------------------------------------------------------------------------------ comments------------------------>
    <div class="bottom">
      <h2 class="lexTitle">Reaction</h2>
      <?php
$query4 = "SELECT * FROM comments WHERE lexid = '$goid'";
$result4 = mysqli_query($con, $query4) or die("Could not get the lex comments");  
$nrows4 = mysqli_num_rows($result4);
while ($row4 = mysqli_fetch_array($result4)){
		extract($row4);
		$ctime = strtotime($commentcreated);
    	$cformatToday = date("D d F Y");
   		$cformatdate  = date("D d F Y", $ctime);
     if($cformatToday == $cformatdate){
        $cformatdate = "today";
    	}
	$cformattime =  date("g:ia", $ctime);	
    $cdatetime = "$cformatdate at $cformattime";	 	
	  ?>
      <div>
        <p  class="highlight">Anon, <?php echo $cdatetime;?>:</p>
        <p><?php echo $row4['comment'];?></p>
      </div>
      <?php } if($allowComment = 1) {  ?>
      <div id="commentsB"> <!-- leave a comment -->
        <form id="comment" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
          <input type='textbox' name='tbComment' id='tbComment'/>
          <input type='hidden' name='tbLexid' id='tbLexid' value="<?php echo $lexid ?>"/>
          <label for='tbComment<?php echo $lexid ?>'></label>
          <button type="submit" id="commentSubmit" class="btn commentSubmit" name="commentSubmit"> Leave comment</button>
        </form>
      </div>
      <?php } 
		  else { ?>
      <p>Comments are disabled for this Lex.</p>
      <?php	  }

		  ?>
    </div>
    <div class="bottom bleft">
   <h2 class="lexTitle">Stats</h2>
   <!--      <p class="block stat">Relection word count <?php echo $reflectionWc ?></p>
      <p class="block stat">Your confidence rating <?php echo $confidenceRating ?></p>
      <h3 class="lexTitle">Context Activity</h3>
      <div class="chart inlineb half">
        <h4 class="nomp lexTitle">Me</h4>
        <div class="bottom bleft"> </div>
        <?php $contextCount = contextCount($userid); ?>
      </div>
      <div class="chart inlineb half">
        <h4 class="nomp lexTitle">Others</h4>
        <div class="bottom bleft"> </div>
        <?php $contextCountAll = contextCountAll(); ?>
      </div>-->
      
      <h3 class="lexTitle">Skill Activity</h3>
      <div class="chart inlineb half">
        <h4 class="nomp lexTitle">Me</h4>
        <div class="bottom bleft"> </div>
        <?php $skillCount = skillCount($userid); ?>
      </div>
      <div class="chart inlineb half">
        <h4 class="nomp lexTitle">Others</h4>
        <div class="bottom bleft"> </div>
        <?php $skillCount = skillCountAll(); ?>
      </div>
   
      <!--individual
a count for each skill
a count for each context
an average confidence rating

group
a count for each skill
a count for each context
an average confidence rating

badge
if all completed
--> 
    </div>
  </div>
  <!-- <div id="subLex" ></div>-->
  <div id="mainlex"> <a id="<?php echo $lexid; ?>"></a> <a id="<?php echo $ltitle; ?>"></a> 
    
    <!--------------------------------------------------------------------------------------------- timedate------------------------>
    <div class='timedate highlight left'><?php echo $datetime ?></div>
    <!--------------------------------------------------------------------------------------------- title------------------------>
    <h2 class="lexTitle"> <?php echo ucfirst($title);?></h2>
    <div class='skillIcons'> 
      <!--------------------------------------------------------------------------------------------- contexts------------------------>
      <?php $query2 = "SELECT DISTINCT source FROM map WHERE lexid = '$goid'";
$result2 = mysqli_query($con, $query2) or die("Could not get the lex comments");  
while($row2 = mysqli_fetch_array($result2)) 
//print_r($row2);
foreach ($row2 as $context){
	$fContext = str_replace(' ', '_', $context);
	if ($context !="My Learning"){
		if ($context =="Academic life"||$context =="Personal life"||$context =="Working life"){ ?>
      <span class="skill inlineb <?php echo $fContext ?>" title="<?php echo $context ?>"><?php echo $context[0] ?></span>
      <?php				
			}	
		}	
	break;	
}      
?>
      <?php
//<!--------------------------------------------------------------------------------------------- skills------------------------>		  
$query3 = "SELECT DISTINCT source FROM map WHERE lexid = '$goid'";
$result3 = mysqli_query($con, $query3) or die("Could not get the lex comments");  
while($row3 = mysqli_fetch_array($result3)) 
foreach ($row3 as $skill){
	$fSkill = str_replace(' ', '_', $skill);
	if($skill == "Critical thinking and problem solving"||$skill == "Creativity and innovation"||$skill == "Collaboration teamwork and leadership"||$skill == "Cross cultural understanding"||$skill == "Communication and media fluency"||$skill == "Computing and ICT fluency"||$skill == "Career and learning self reliance"){
	?>
      <span class="skill inlineb <?php echo $fSkill ?>" title="<?php echo $skill ?>"></span>
      <?php
			}	
	break;	
}	?>
    </div>
    <!--------------------------------------------------------------------------------------------- tags-------------------------> 
    
    <a href="<?php echo '?id='. $lexid; ?>"> </a>
    <div class = 'block'>
      <?php $queryTags = "SELECT DISTINCT tag FROM tags WHERE lexid = '$goid'";
$resultTags = mysqli_query($con, $queryTags) or die("Could not get the lex comments");  
$nrowTags = mysqli_num_rows($resultTags);
while($rowTags = mysqli_fetch_array($resultTags))
foreach ($rowTags as $tag){
?>
      <a href ="<?php echo $tag ?>" class="inlineb"><?php echo $tag ?></a>
      <?php	break;	}	

        ?>
    </div>
    <!---------------------------------------------------------------------------------------------reflection------------------------->
    <div class= 'block reflection'>
      <p class="reflection"><?php echo $reflection;?></p>
    </div>
    <!---------------------------------------------------------------------------------------------files------------------------->
    <div class = 'block'>
<!--    <?php echo $fileurl;?>-->
<!--     <form id="download" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <button type="submit"  name="getfile">download</button></form>-->
    
	
	<?php echo $getFiles = getFiles($lexid);?>
     </div>
    <?php
//if there are comments show them
$query4 = "SELECT * FROM comments WHERE lexid = '$goid'";
$result4 = mysqli_query($con, $query4) or die("Could not get the lex comments");  
$nrows4 = mysqli_num_rows($result4);
?>
    <!----------------------------------------------------------------------------------------------sharing------------------------>
    <P>Why not share this lex with your employer, tutor or friends (Dummy account)</P>
    <div class="addthis_sharing_toolbox sharing block ">
      <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-546a118f5504785f" async="async"></script> 
      
    </div>
  </div>
</div>
</html>